package spt.slides;

import java.sql.Timestamp;
import java.util.logging.Logger;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.apache.commons.lang3.StringUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import spt.utils.XMLUtils;

public class SlideXMLReader {
	private static final Logger log = Logger.getLogger(SlideXMLReader.class.getName());
	
	public static Slide read(String xml){
		Document document = XMLUtils.parseDocument(xml);
		Element slideElement = (Element)document.getElementsByTagName("slide").item(0);
		Slide slide = new Slide();
		
		String id = slideElement.getAttribute("id");
		String content = getContentAsString(xml);
		String publishStart = slideElement.getAttribute("publish-start");
		String publishStop = slideElement.getAttribute("publish-stop");
		String displayTime = slideElement.getAttribute("display-time");
		String alwaysPublished = slideElement.getAttribute("always-published");
		
		if(!id.isEmpty())
			slide.setId(Integer.parseInt(id));
		
		if(!content.isEmpty())
			slide.setContent(content);
		
		if(!publishStart.isEmpty())
			slide.setPublishStart(Timestamp.valueOf(publishStart));
		
		if(!publishStop.isEmpty())
			slide.setPublishStop(Timestamp.valueOf(publishStop));
		
		if(!displayTime.isEmpty())
			slide.setDisplayTime(Integer.parseInt(displayTime));
		
		if(!alwaysPublished.isEmpty())
			slide.setAlwaysPublished(Boolean.parseBoolean(alwaysPublished));
		else
			slide.setAlwaysPublished(false);
		
		return slide;
	}
	
//	public static String getContentAsString(Element slideElement){
//		StringBuffer content = new StringBuffer();
//		Element title = (Element)slideElement.getElementsByTagName("title").item(0);
//		Element text = (Element)slideElement.getElementsByTagName("text").item(0);
//		NodeList images = slideElement.getElementsByTagName("image");
//		NodeList videos = slideElement.getElementsByTagName("video");
//		
//		if(title != null)
//			content.append("<title>"+title.getTextContent()+"</title>");
//		if(text != null)
//			content.append("<text>"+text.getTextContent()+"</text>");
//		for(int i = 0; i < images.getLength(); i++)
//			content.append("<image>"+images.item(i).getTextContent()+"</image>");
//		for(int i = 0; i < videos.getLength(); i++)
//			content.append("<videos>"+videos.item(i).getTextContent()+"</videos>");
//		return content.toString();
//		
//	}
	
	public static String getContentAsString(String xml){
		String s = StringUtils.substringBetween(xml, "\">", "</slide>");
		if(s == null || s.isEmpty())
			s = StringUtils.substringBetween(xml, "\" >", "</slide>");
		return s;
	}
	
	public static String getContentAsString(Document document){ 
		XPath xpath = XPathFactory.newInstance().newXPath();
		try {
			XPathExpression expr = xpath.compile("//slide/*");
			NodeList content = (NodeList)expr.evaluate(document, XPathConstants.NODESET);
			
			Document contentDoc = XMLUtils.newDocument();
			for(int i = 0; i < content.getLength(); i++)
				contentDoc.appendChild(contentDoc.adoptNode(content.item(i)));
			
			return XMLUtils.transformToString(contentDoc, true);
		} catch (XPathExpressionException e) {
			log.severe(e.getMessage());
			return null;
		}
	}
}
